// ButtonStatic.cpp : implementation file
//

#include "stdafx.h"
#include "ButtonStatic.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CButtonStatic

CButtonStatic::CButtonStatic():
m_bCanChecked(false),
m_bCheck(false)
{
  m_pBitmapUp = new CBitmap;
  m_pBitmapDown = new CBitmap;
}

CButtonStatic::~CButtonStatic()
{
  m_pBitmapUp->DeleteObject();
  m_pBitmapDown->DeleteObject();
  delete m_pBitmapUp;
  delete m_pBitmapDown;
}


BEGIN_MESSAGE_MAP(CButtonStatic, CStatic)
	//{{AFX_MSG_MAP(CButtonStatic)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CButtonStatic message handlers

void CButtonStatic::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
  if (m_bCanChecked)
  {
    CheckButton(!m_bCheck);
  }

 	CStatic::OnLButtonDown(nFlags, point);
}

bool CButtonStatic::LoadBitmaps(LPCTSTR lpszBitmapResource, LPCTSTR lpszBitmapResourceChecked, bool bCanChecked)
{
  m_bCanChecked = bCanChecked;

  return m_pBitmapUp->LoadBitmap(lpszBitmapResource) && m_pBitmapDown->LoadBitmap(lpszBitmapResourceChecked);
}

bool CButtonStatic::LoadBitmaps(UINT nIDBitmapResource, UINT nIDBitmapResourceChecked, bool bCanChecked)
{
  m_bCanChecked = bCanChecked;

  return m_pBitmapUp->LoadBitmap(nIDBitmapResource) && m_pBitmapDown->LoadBitmap(nIDBitmapResourceChecked);
}

void CButtonStatic::CheckButton(bool bCheck)
{
  m_bCheck = bCheck;
  SetBitmap((HBITMAP)(m_bCheck?m_pBitmapDown->GetSafeHandle():m_pBitmapUp->GetSafeHandle()));
}

void CButtonStatic::SetToolTip(LPCTSTR lpszToolTip)
{
  if (IsWindow(m_tooltip.m_hWnd))
  {
    CToolInfo ti;
    m_tooltip.GetToolInfo(ti, this);
    ti.lpszText = (LPTSTR)lpszToolTip;
    m_tooltip.SetToolInfo(&ti);
  }
  else
  {
    m_tooltip.Create(this);
    TOOLINFO ti;
    ZeroMemory(&ti, sizeof(TOOLINFO));
    ti.cbSize = sizeof(TOOLINFO);
    ti.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
    ti.uId = (UINT)GetSafeHwnd();
    ti.lpszText = (LPTSTR)lpszToolTip;
    m_tooltip.SendMessage (TTM_ADDTOOL, 0, (LPARAM) &ti);
  }
}
